/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 */
 var BSTIterator = function(root) {
    this.seq=new Array();
    traverse=(node) => 
    {
        if(node.left){
            traverse(node.left);
        }
        this.seq.push(node.val);
        if(node.right){
            traverse(node.right);
        }
    }
    traverse(root);
    this.iterator=function *(seq){
        for(var i=0;i<seq.length-1;i++){
            yield seq[i];
        }
        return seq[i];
    }(this.seq);
    this.finish=true;
};

/**
 * @return {number}
 */
BSTIterator.prototype.next = function() {
    let re=this.iterator.next();
    this.finish=!re.done;
    return re.value;
};

/**
 * @return {boolean}
 */
BSTIterator.prototype.hasNext = function() {
    return this.finish;
};

/**
 * Your BSTIterator object will be instantiated and called as such:
 * var obj = new BSTIterator(root)
 * var param_1 = obj.next()
 * var param_2 = obj.hasNext()
 */


//  执行用时：
//  200 ms
//  , 在所有 JavaScript 提交中击败了
//  10.19%
//  的用户
//  内存消耗：
//  48.4 MB
//  , 在所有 JavaScript 提交中击败了
//  99.36%
//  的用户